package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.dao.Pocess;
import com.qinghe.plant.model.PocessResponsitory;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;

@RestController
public class PocessController{
	
	@Autowired
	private PocessResponsitory pocessResponsitory;
	
	@RequestMapping(value = "doReadPocesss", method = {RequestMethod.GET})
    public Response ReadPocesss(HttpServletRequest request) 
	{
		List<Pocess> pocesss=pocessResponsitory.findAll();		
		String content=JSONArray.toJSONString(pocesss).toString();
		return new Response(CodeType.Success,content,"操作成功");
	}
	
	@RequestMapping(value = "doReadPocess", method = {RequestMethod.POST})
    public Response ReadPocess(HttpServletRequest request) 
	{
		String id=request.getParameter("id");
		Pocess pocess=FindPocessById(Integer.parseInt(id));
		return new Response(CodeType.Success,JSONObject.toJSONString(pocess),"操作成功");
	}
	
	@RequestMapping(value = "doAlterPocess", method = {RequestMethod.POST})
	public Response AlterPocess(HttpServletRequest request){		
		Pocess pocess=JSONObject.parseObject(request.getParameter("pocess"),Pocess.class);
		pocess= pocessResponsitory.save(pocess);
		return new Response(CodeType.Success,JSONObject.toJSONString(pocess),"操作成功");
	}

	@RequestMapping(value = "doInsertPocess", method = {RequestMethod.POST})
	public Response InsertPocess(HttpServletRequest request)
	{		
		Pocess pocess=new Pocess();
		pocess.setPid(request.getParameter("id"));
		pocess.setOid(request.getParameter("WorkOrderId"));
		pocess.setSerial(request.getParameter("Seq"));
		pocess.setName(request.getParameter("Name"));
		pocess.setCount(Double.parseDouble(request.getParameter("PutInQty")));
		//pocess.setPsdate(request.getParameter("PlanStartDate"));
		//pocess.setPfdate(request.getParameter("PlanFinishDate"));
		//pocess.setAsdate(request.getParameter("ActStartDate"));
		//pocess.setAfdate(request.getParameter("ActStartDate"));
		pocess.setState(request.getParameter("Status"));
		pocess.setTimer(new Date());
		pocess= pocessResponsitory.save(pocess);
		return new Response(CodeType.Success,JSONObject.toJSONString(pocess),"操作成功");	
	}
	
	@RequestMapping(value = "doDeletePocess", method = {RequestMethod.POST})
	public Response DeletePocess(HttpServletRequest request)
	{
		Pocess pocess=JSONObject.parseObject(request.getParameter("pocess"),Pocess.class);
		pocessResponsitory.deleteById(pocess.getId());	
		return new Response(CodeType.Success,null,"操作成功");
	}	
	
	public void InsetData()
	{
		System.out.println("插入一次Station数据");
	}
	
	public void DeleteData(Date date)
	{
		System.out.println("删除一次Station数据");
	}
	
	private Pocess FindPocessById(Integer id) {
		@SuppressWarnings("serial")
		List<Pocess> result = pocessResponsitory.findAll(new Specification<Pocess>() {
			@Override
			public Predicate toPredicate(Root<Pocess> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("id").as(Integer.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
				}
		});
		return (result.size()!=0)?result.get(0):null;
	}
		
}
